package com.example.news.service.impl;

import com.example.news.mapper.NewsMapper;
import com.example.news.pojo.Comments;
import com.example.news.pojo.News;
import com.example.news.pojo.PageResult;
import com.example.news.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    private Integer pageSize = 3;

    @Override
    public PageResult<News> getAllNews(Integer currentPage, String title) {
        System.out.println("进入ServiceImpl...");
        //设置页码和页面大小
        PageHelper.startPage(currentPage,pageSize);
        //查询结果
        List<News> newsList = newsMapper.getAllNews(currentPage, title);
        //分页查询
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        //封装查询结果
        PageResult<News> pageResult =new PageResult<>();
        pageResult.setCurrentPage(currentPage);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalCount((int)pageInfo.getTotal());
        pageResult.setTotalPages(pageInfo.getPages());
        pageResult.setResultList(pageInfo.getList());
        return pageResult;
    }

    @Override
    public News showNewsDetail(String newsCode) {
        return newsMapper.showNewsDetail(newsCode);
    }

    @Override
    public List<Comments> getComments(String newsCode) {
        return newsMapper.getComments(newsCode);
    }

    @Override
    public Integer addComments(Comments comments) {
        return newsMapper.addComments(comments);
    }
}
